package com.itany.controller;


import com.github.pagehelper.PageInfo;
import com.itany.constant.ResponseConstant;
import com.itany.exception.AlreadyExaminedException;
import com.itany.exception.RequestParamsException;
import com.itany.pojo.Examine;
import com.itany.service.ExamineService;
import com.itany.vo.ActionResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 图书审核
 */
@Controller
@RequestMapping("/examine")
public class ExamineController {

    @Autowired
    ExamineService examineService;

    @RequestMapping("/findAll")
    @ResponseBody
    public Map<String, Object> findAll(@RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "10") Integer rows,
                                       String booknameKey, String ISBNKey,
                                       @RequestParam(required = false) String supplierId, HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        PageInfo<Examine> info = null;
        try {
            info = examineService.findAll(page, rows, booknameKey, ISBNKey, supplierId,session);
        } catch (RequestParamsException e) {
            e.printStackTrace();
        }
        map.put("total", info.getTotal());
        map.put("rows", info.getList());
        return map;
    }


    /**
     * 审核图书信息
     *
     * @param examineStatus 审核结果
     * @param examineDetail 审核备注
     * @param examineId     审核条目序号
     * @return 响应数据
     */
    @RequestMapping("/examineBook")
    @ResponseBody
    public ActionResult enable(String examineStatus, String examineDetail, String examineId) {
        ActionResult result = new ActionResult();
        try {
            examineService.examineBook(examineStatus, examineDetail, examineId);
            result.setStatus(ResponseConstant.RESPONSE_SUCCESS);
        } catch (RequestParamsException | AlreadyExaminedException e) {
            e.printStackTrace();
            result.setMsg(e.getMessage());
            result.setStatus(ResponseConstant.REQUEST_PARAMETER_ERROR);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("服务器内部异常");
            result.setStatus(ResponseConstant.SERVER_ERROR);
        }
        return result;
    }

}
